﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FridgeManagerApp
{
    public class PermissionList
    {
        private readonly CStudy[] STUDIES = new CStudy[]
        {
            new CStudy{ StudyUID = EStudyUID.Personnel, Title = "员工设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.PersonnelGroup, Title = "用户组设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Department, Title = "营业部设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Permission, Title = "权限设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.User, Title = "用户设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Domino, Title = "商品设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.Device, Title = "设备设置", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.StockNote, Title = "进货记录", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.ReplenishmentNote, Title = "补货记录", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.SoldNote, Title = "消费记录", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CStudy{ StudyUID = EStudyUID.ExceptionNote, Title = "异常商品记录", Icon = "", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
        };

        private readonly CSeries[] SERIESES = new CSeries[]
        {
            new CSeries{SeriesUID = ESeriesUID.Personnel, Title = "员工列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.PersonnelGroup, Title = "用户组列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.BD_Area, Title = "大区列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.BD_Province, Title = "省列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.BD_City, Title = "城市列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.BD_District, Title = "区列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.BD_Office, Title = "营业部列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.Permission, Title = "权限列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.User, Title = "用户列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.Domino, Title = "商品列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DominoType, Title = "商品类型列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.Device, Title = "设备列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DeviceType, Title = "设备类型列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DeviceCell, Title = "设备格子编号列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DeviceOfficeNexus, Title = "设备营业部关系表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.DeviceStatus, Title = "设备状态列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.StockNote, Title = "进货记录列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.ReplenishmentNote, Title = "补货记录列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.SoldNote, Title = "消费记录列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
            new CSeries{SeriesUID = ESeriesUID.ExceptionNote, Title = "异常商品记录列表", StatusNormal = "", StatusEnter = "", StatusDown = "", StatuseEnable = ""},
        };

        private readonly CImage[] IMAGES = new CImage[]
        {
            new CImage{StudyUID = EStudyUID.Personnel, SeriesUID = ESeriesUID.Personnel, ImageUID = EImageUID.PersonnelAdd},
            new CImage{StudyUID = EStudyUID.Personnel, SeriesUID = ESeriesUID.Personnel, ImageUID = EImageUID.PersonnelRead},
            new CImage{StudyUID = EStudyUID.Personnel, SeriesUID = ESeriesUID.Personnel, ImageUID = EImageUID.PersonnelEdit},
            new CImage{StudyUID = EStudyUID.Personnel, SeriesUID = ESeriesUID.Personnel, ImageUID = EImageUID.PersonnelDelete},
            new CImage{StudyUID = EStudyUID.PersonnelGroup, SeriesUID = ESeriesUID.PersonnelGroup, ImageUID = EImageUID.PersonnelGroupAdd},
            new CImage{StudyUID = EStudyUID.PersonnelGroup, SeriesUID = ESeriesUID.PersonnelGroup, ImageUID = EImageUID.PersonnelGroupRead},
            new CImage{StudyUID = EStudyUID.PersonnelGroup, SeriesUID = ESeriesUID.PersonnelGroup, ImageUID = EImageUID.PersonnelGroupEdit},
            new CImage{StudyUID = EStudyUID.PersonnelGroup, SeriesUID = ESeriesUID.PersonnelGroup, ImageUID = EImageUID.PersonnelGroupDelete},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Area, ImageUID = EImageUID.BD_AreaAdd},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Area, ImageUID = EImageUID.BD_AreaRead},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Area, ImageUID = EImageUID.BD_AreaEdit},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Area, ImageUID = EImageUID.BD_AreaDelete},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Province, ImageUID = EImageUID.BD_ProvinceAdd},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Province, ImageUID = EImageUID.BD_ProvinceRead},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Province, ImageUID = EImageUID.BD_ProvinceEdit},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Province, ImageUID = EImageUID.BD_ProvinceDelete},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_City, ImageUID = EImageUID.BD_CityAdd},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_City, ImageUID = EImageUID.BD_CityRead},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_City, ImageUID = EImageUID.BD_CityEdit},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_City, ImageUID = EImageUID.BD_CityDelete},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_District, ImageUID = EImageUID.BD_DistrictAdd},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_District, ImageUID = EImageUID.BD_DistrictRead},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_District, ImageUID = EImageUID.BD_DistrictEdit},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_District, ImageUID = EImageUID.BD_DistrictDelete},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Office, ImageUID = EImageUID.BD_OfficeAdd},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Office, ImageUID = EImageUID.BD_OfficeRead},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Office, ImageUID = EImageUID.BD_OfficeEdit},
            new CImage{StudyUID = EStudyUID.Department, SeriesUID = ESeriesUID.BD_Office, ImageUID = EImageUID.BD_OfficeDelete},
            new CImage{StudyUID = EStudyUID.Permission, SeriesUID = ESeriesUID.Permission, ImageUID = EImageUID.PermissionAdd},
            new CImage{StudyUID = EStudyUID.Permission, SeriesUID = ESeriesUID.Permission, ImageUID = EImageUID.PermissionRead},
            new CImage{StudyUID = EStudyUID.Permission, SeriesUID = ESeriesUID.Permission, ImageUID = EImageUID.PermissionEdit},
            new CImage{StudyUID = EStudyUID.Permission, SeriesUID = ESeriesUID.Permission, ImageUID = EImageUID.PermissionDelete},
            new CImage{StudyUID = EStudyUID.User, SeriesUID = ESeriesUID.User, ImageUID = EImageUID.UserAdd},
            new CImage{StudyUID = EStudyUID.User, SeriesUID = ESeriesUID.User, ImageUID = EImageUID.UserRead},
            new CImage{StudyUID = EStudyUID.User, SeriesUID = ESeriesUID.User, ImageUID = EImageUID.UserEdit},
            new CImage{StudyUID = EStudyUID.User, SeriesUID = ESeriesUID.User, ImageUID = EImageUID.UserDelete},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.Domino, ImageUID = EImageUID.DominoAdd},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.Domino, ImageUID = EImageUID.DominoRead},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.Domino, ImageUID = EImageUID.DominoEdit},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.Domino, ImageUID = EImageUID.DominoDelete},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.DominoType, ImageUID = EImageUID.DominoTypeAdd},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.DominoType, ImageUID = EImageUID.DominoTypeRead},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.DominoType, ImageUID = EImageUID.DominoTypeEdit},
            new CImage{StudyUID = EStudyUID.Domino, SeriesUID = ESeriesUID.DominoType, ImageUID = EImageUID.DominoTypeDelete},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.Device, ImageUID = EImageUID.DeviceAdd},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.Device, ImageUID = EImageUID.DeviceRead},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.Device, ImageUID = EImageUID.DeviceEdit},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.Device, ImageUID = EImageUID.DeviceDelete},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceType, ImageUID = EImageUID.DeviceTypeAdd},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceType, ImageUID = EImageUID.DeviceTypeRead},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceType, ImageUID = EImageUID.DeviceTypeEdit},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceType, ImageUID = EImageUID.DeviceTypeDelete},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceCell, ImageUID = EImageUID.DeviceCellAdd},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceCell, ImageUID = EImageUID.DeviceCellRead},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceCell, ImageUID = EImageUID.DeviceCellEdit},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceCell, ImageUID = EImageUID.DeviceCellDelete},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceOfficeNexus, ImageUID = EImageUID.DeviceOfficeNexusAdd},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceOfficeNexus, ImageUID = EImageUID.DeviceOfficeNexusRead},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceOfficeNexus, ImageUID = EImageUID.DeviceOfficeNexusEdit},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceOfficeNexus, ImageUID = EImageUID.DeviceOfficeNexusDelete},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceStatus, ImageUID = EImageUID.DeviceStatusAdd},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceStatus, ImageUID = EImageUID.DeviceStatusRead},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceStatus, ImageUID = EImageUID.DeviceStatusEdit},
            new CImage{StudyUID = EStudyUID.Device, SeriesUID = ESeriesUID.DeviceStatus, ImageUID = EImageUID.DeviceStatusDelete},
            new CImage{StudyUID = EStudyUID.StockNote, SeriesUID = ESeriesUID.StockNote, ImageUID = EImageUID.StockNoteAdd},
            new CImage{StudyUID = EStudyUID.StockNote, SeriesUID = ESeriesUID.StockNote, ImageUID = EImageUID.StockNoteRead},
            new CImage{StudyUID = EStudyUID.StockNote, SeriesUID = ESeriesUID.StockNote, ImageUID = EImageUID.StockNoteEdit},
            new CImage{StudyUID = EStudyUID.StockNote, SeriesUID = ESeriesUID.StockNote, ImageUID = EImageUID.StockNoteDelete},
            new CImage{StudyUID = EStudyUID.ReplenishmentNote, SeriesUID = ESeriesUID.ReplenishmentNote, ImageUID = EImageUID.ReplenishmentNoteAdd},
            new CImage{StudyUID = EStudyUID.ReplenishmentNote, SeriesUID = ESeriesUID.ReplenishmentNote, ImageUID = EImageUID.ReplenishmentNoteRead},
            new CImage{StudyUID = EStudyUID.ReplenishmentNote, SeriesUID = ESeriesUID.ReplenishmentNote, ImageUID = EImageUID.ReplenishmentNoteEdit},
            new CImage{StudyUID = EStudyUID.ReplenishmentNote, SeriesUID = ESeriesUID.ReplenishmentNote, ImageUID = EImageUID.ReplenishmentNoteDelete},
            new CImage{StudyUID = EStudyUID.SoldNote, SeriesUID = ESeriesUID.SoldNote, ImageUID = EImageUID.SoldNoteAdd},
            new CImage{StudyUID = EStudyUID.SoldNote, SeriesUID = ESeriesUID.SoldNote, ImageUID = EImageUID.SoldNoteRead},
            new CImage{StudyUID = EStudyUID.SoldNote, SeriesUID = ESeriesUID.SoldNote, ImageUID = EImageUID.SoldNoteEdit},
            new CImage{StudyUID = EStudyUID.SoldNote, SeriesUID = ESeriesUID.SoldNote, ImageUID = EImageUID.SoldNoteDelete},
            new CImage{StudyUID = EStudyUID.ExceptionNote, SeriesUID = ESeriesUID.ExceptionNote, ImageUID = EImageUID.ExceptionNoteAdd},
            new CImage{StudyUID = EStudyUID.ExceptionNote, SeriesUID = ESeriesUID.ExceptionNote, ImageUID = EImageUID.ExceptionNoteRead},
            new CImage{StudyUID = EStudyUID.ExceptionNote, SeriesUID = ESeriesUID.ExceptionNote, ImageUID = EImageUID.ExceptionNoteEdit},
            new CImage{StudyUID = EStudyUID.ExceptionNote, SeriesUID = ESeriesUID.ExceptionNote, ImageUID = EImageUID.ExceptionNoteDelete}
        };

        private static PermissionList permission;
        public static PermissionList GetInstance()
        {
            if(permission == null)  permission = new PermissionList();

            return permission;
        }

        private PermissionList()
        {
            CreatePermissions();
        }

        private List<String> Permissions = new List<String>();

        //DEBUG
        public void CreatePermissions()
        {
            for (int i = 0; i < EImageUID.Images.Count(); i++)
            {
                Permissions.Add(EImageUID.Images[i]);
            }
        }

        public List<CStudy> GetStudies()
        {
            List<CStudy> studies = new List<CStudy>();

            foreach (String permission in Permissions)
            {
                foreach (CImage image in IMAGES)
                {
                    if (permission == image.ImageUID)
                    {
                        bool isExist = false;

                        foreach (CStudy study in studies)
                        {
                            if (image.StudyUID == study.StudyUID)
                            {
                                isExist = true;
                                break;
                            }
                        }

                        if (!isExist)
                        {
                            studies.Add(STUDIES[(int)image.StudyUID]);
                        }

                        break;
                    }
                }
            }

            return studies;
        }

        public List<CSeries> GetSerieses(EStudyUID pEStudyUID)
        {
            List<CSeries> serieses = new List<CSeries>();

            foreach (String permission in Permissions)
            {
                foreach (CImage image in IMAGES)
                {
                    if (permission == image.ImageUID && pEStudyUID == image.StudyUID)
                    {
                        bool isExist = false;

                        foreach (CSeries series in serieses)
                        {
                            if (image.SeriesUID == series.SeriesUID)
                            {
                                isExist = true;
                                break;
                            }
                        }

                        if (!isExist)
                        {
                            serieses.Add(SERIESES[(int)image.SeriesUID]);
                        }

                        break;
                    }
                }
            }

            return serieses;
        }

        public List<CImage> GetImages(ESeriesUID pESeriesUID)
        {
            List<CImage> images = new List<CImage>();

            foreach (String permission in Permissions)
            {
                foreach (CImage image in IMAGES)
                {
                    if (permission == image.ImageUID && pESeriesUID == image.SeriesUID)
                    {
                        bool isExist = false;

                        foreach (CImage img in images)
                        {
                            if (image.ImageUID == img.ImageUID)
                            {
                                isExist = true;
                                break;
                            }
                        }

                        if (!isExist)
                        {
                            images.Add(image);
                        }

                        break;
                    }
                }
            }

            return images;
        }

        public EStudyUID GetStudyUID(ESeriesUID pESeriesUID)
        {
            foreach (CImage image in IMAGES)
            {
                if (image.SeriesUID == pESeriesUID)
                {
                    return image.StudyUID;
                }
            }

            return EStudyUID.None;
        }

        public EStudyUID GetStudyUID(String pEImageUID)
        {
            foreach (CImage image in IMAGES)
            {
                if (image.ImageUID == pEImageUID)
                {
                    return image.StudyUID;
                }
            }

            return EStudyUID.None;
        }

        public ESeriesUID GetSeriesUID(String pEImageUID)
        {
            foreach (CImage image in IMAGES)
            {
                if (image.ImageUID == pEImageUID)
                {
                    return image.SeriesUID;
                }
            }

            return ESeriesUID.None;
        }

        public CStudy GetStudy(EStudyUID pEStudyUID)
        {
            CStudy study = null;

            foreach (var std in STUDIES)
            {
                if (std.StudyUID == pEStudyUID)
                {
                    study = std;
                    break;
                }
            }

            return study;
        }

        public CSeries GetSeries(ESeriesUID pESeriesUID)
        {
            CSeries series = null;

            foreach (var srs in SERIESES)
            {
                if (srs.SeriesUID == pESeriesUID)
                {
                    series = srs;
                    break;
                }
            }

            return series;
        }
    }
}
